package weeek12.topic05;

import com.hazelcast.config.Config;
import com.hazelcast.config.PartitionGroupConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

import java.util.Map;

public class HazelcastClusterTest {
    public static void main(String[] args) {
        clusterDemo();
    }
    /**
     * 搭建 hazelcast 3 节点集群，写入 100 万数据到一个 map，模拟和演 示高可用
     */
    private static void clusterDemo() {
        Config config = new Config();
        config.setClusterName("test_cluster");
        //如果有其它实例，这里可以替换
        HazelcastInstance hazelcastInstance1 = Hazelcast.newHazelcastInstance(config);
        HazelcastInstance hazelcastInstance2 = Hazelcast.newHazelcastInstance(config);
        HazelcastInstance hazelcastInstance3 = Hazelcast.newHazelcastInstance(config);

        Map<Integer, Integer> map1 = hazelcastInstance1.getMap("data");
        Map<Integer, Integer> map2 = hazelcastInstance2.getMap("data");
        Map<Integer, Integer> map3 = hazelcastInstance3.getMap("data");
        System.out.println("init success");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            map1.put(i, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("insert cost:"+(end-start));
        for (int i = 0; i < 10; i++) {
            System.out.println(map2.get(i));
            System.out.println(map3.get(i));
        }
    }
}
